/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bank.gui;

import bank.core.BankAccountFactory;
import bank.core.Bank;
import bank.core.CheckingAccount;
import bank.core.PartyFactory;
import bank.core.SavingAccount;
import bank.entries.AddInterest;
import bank.entries.Deposit;
import bank.entries.Withdrawal;
import frmwrk.core.CustomerFactory;
import frmwrk.core.FinancialFramework;
import frmwrk.core.Organization;
import frmwrk.core.Person;
import frmwrk.interfaces.IAccount;
import frmwrk.interfaces.IEntry;
import java.awt.event.ActionEvent;
import java.util.Observable;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import gui.core.*;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Observer;
import javax.swing.JTextArea;

/**
 *
 * @author Damian
 */
public class BankFrm extends gui.core.CoreFrame {

    javax.swing.JButton JButton_PerAC;
    javax.swing.JButton JButton_CompAC;
    javax.swing.JButton JButton_Deposit;
    javax.swing.JButton JButton_Withdraw;
    javax.swing.JButton JButton_Addinterest;
    javax.swing.JButton JButton_Exit;
    javax.swing.JButton JButton_GenBill;
    Bank ff;

    public BankFrm(Bank financialFramework) {
        ff = financialFramework;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        Object object = event.getSource();
        if (object == JButton_Exit) {
            JButtonExit_actionPerformed(event);
        } else if (object == JButton_PerAC) {
            JButtonPerAC_actionPerformed(event);
        } else if (object == JButton_CompAC) {
            JButtonCompAC_actionPerformed(event);
        } else if (object == JButton_Deposit) {
            JButtonDeposit_actionPerformed(event);
        } else if (object == JButton_Withdraw) {
            JButtonWithdraw_actionPerformed(event);
        } else if (object == JButton_Addinterest) {
            JButtonAddinterest_actionPerformed(event);
        } else if (object == JButton_GenBill) {
            JButtonGenBill_actionPerformed(event);
        }
    }

    @Override
    protected JButton[] makeButtons() {

        JButton_PerAC = new javax.swing.JButton();
        JButton_CompAC = new javax.swing.JButton();
        JButton_Deposit = new javax.swing.JButton();
        JButton_Withdraw = new javax.swing.JButton();
        JButton_Addinterest = new javax.swing.JButton();
        JButton_Exit = new javax.swing.JButton();
        JButton_GenBill = new javax.swing.JButton();


        JButton_PerAC.setText("Add personal account");

        JButton_PerAC.setBounds(24, 20, 192, 33);
        JButton_PerAC.setActionCommand("PerAC");

        JButton_CompAC.setText("Add company account");
        JButton_CompAC.setActionCommand("CompAC");

        JButton_CompAC.setBounds(240, 20, 192, 33);
        JButton_Deposit.setText("Deposit");

        JButton_Deposit.setBounds(468, 104, 96, 33);
        JButton_Withdraw.setText("Withdraw");

        JButton_Addinterest.setBounds(448, 20, 106, 33);
        JButton_Addinterest.setText("Add interest");

        JButton_Withdraw.setBounds(468, 164, 96, 33);
        JButton_Exit.setText("Exit");

        JButton_Exit.setBounds(468, 248, 96, 33);

        JButton_GenBill.setBounds(468, 204, 96, 33);
        JButton_GenBill.setText("Report");


        JButton_Exit.addActionListener(this);
        JButton_PerAC.addActionListener(this);
        JButton_CompAC.addActionListener(this);
        JButton_Deposit.addActionListener(this);
        JButton_Withdraw.addActionListener(this);
        JButton_Addinterest.addActionListener(this);
        JButton_GenBill.addActionListener(this);


        return new JButton[]{JButton_PerAC, JButton_CompAC, JButton_Deposit, JButton_Withdraw,
                    JButton_Addinterest, JButton_Withdraw, JButton_Exit, JButton_GenBill
                };
    }

    @Override
    protected DefaultTableModel makeTable() {
        DefaultTableModel model = new DefaultTableModel();

        model.addColumn("AccountNr");
        model.addColumn("Name");
        model.addColumn("City");
        model.addColumn("P/C");
        model.addColumn("Ch/S");
        model.addColumn("Amount");
        rowdata = new Object[8];
        newaccount = false;
        return model;





    }

    public BankFrm() {
        super.setTitle("Bank application");

        SymWindow aSymWindow = new SymWindow();
        this.addWindowListener(aSymWindow);


    }

    void JButtonExit_actionPerformed(java.awt.event.ActionEvent event) {
        System.exit(0);
    }

    void JButtonPerAC_actionPerformed(java.awt.event.ActionEvent event) {
        /*
        JDialog_AddPAcc type object is for adding personal information
        construct a JDialog_AddPAcc type object
        set the boundaries and show it
         */

        JDialog_AbstractAddAcc pac = new JDialog_AddPAcc(this);
        pac.setBounds(450, 20, 300, 330);
        pac.show();

        if (newaccount) {
            // add row to table
            HashMap<String, String> accountMap = new HashMap<String, String>();
            HashMap<String, String> partyMap = new HashMap<String, String>();

            accountMap.put("accountNumber", accountnr);
            accountMap.put("balance", "0");
            accountMap.put("accountType", accountType);
            accountMap.put("partyType", "Person");

            partyMap.put("name", clientName);
            partyMap.put("street", street);
            partyMap.put("city", city);
            partyMap.put("state", state);
            partyMap.put("zip", zip);
            partyMap.put("email", email);
            partyMap.put("birthdate", birthdate);
            partyMap.put("type", "Person");

            rowdata[0] = accountnr;
            rowdata[1] = clientName;
            rowdata[2] = city;
            rowdata[3] = "P";
            rowdata[4] = accountType;
            rowdata[5] = "0";
            model.addRow(rowdata);
            JTable1.getSelectionModel().setAnchorSelectionIndex(-1);
            newaccount = false;
            List<Observer> observers = new ArrayList<Observer>();
            observers.add(this);
            ff.createAccount( partyMap,accountMap,observers, new BankAccountFactory(), new CustomerFactory());

        }



    }

    void JButtonCompAC_actionPerformed(java.awt.event.ActionEvent event) {
        /*
        construct a JDialog_AddCompAcc type object
        set the boundaries and
        show it
         */

        JDialog_AddCompAcc pac = new JDialog_AddCompAcc(this);
        pac.setBounds(450, 20, 300, 330);
        pac.show();

        if (newaccount) {
            // add row to table
            HashMap<String, String> accountMap = new HashMap<String, String>();
            HashMap<String, String> partyMap = new HashMap<String, String>();

            accountMap.put("accountNumber", accountnr);
            accountMap.put("balance", "0");
            accountMap.put("accountType", accountType);
            accountMap.put("partyType", "Organization");

            partyMap.put("name", clientName);
            partyMap.put("street", street);
            partyMap.put("city", city);
            partyMap.put("state", state);
            partyMap.put("zip", zip);
            partyMap.put("email", email);
            partyMap.put("numberOfEmployees", numberOfEmployees);
            partyMap.put("type", "Organization");

            rowdata[0] = accountnr;
            rowdata[1] = clientName;
            rowdata[2] = city;
            rowdata[3] = "C";
            rowdata[4] = accountType;
            rowdata[5] = "0";
            model.addRow(rowdata);
            JTable1.getSelectionModel().setAnchorSelectionIndex(-1);
            newaccount = false;

            List<Observer> observers = new ArrayList<Observer>();
            observers.add(this);
            ff.createAccount( partyMap,accountMap,observers, new BankAccountFactory(), new CustomerFactory());

        }

    }

    void JButtonDeposit_actionPerformed(java.awt.event.ActionEvent event) {
        // get selected name
        int selection = JTable1.getSelectionModel().getMinSelectionIndex();
        if (selection >= 0) {
            String accnr = (String) model.getValueAt(selection, 0);

            //Show the dialog for adding deposit amount for the current mane
            JDialog_AbstractEntry dep = new JDialog_Deposit(this, accnr);
            dep.setBounds(430, 15, 275, 140);
            dep.show();

            // compute new amount
            Double deposit = Double.parseDouble(amountDeposit);
            String accNumber = (String) model.getValueAt(selection, 0);
            ff.addDepositEntry(accNumber, deposit);
 
        }


    }

    void JButtonWithdraw_actionPerformed(java.awt.event.ActionEvent event) {
        // get selected name
        int selection = JTable1.getSelectionModel().getMinSelectionIndex();
        if (selection >= 0) {
            String accnr = (String) model.getValueAt(selection, 0);

            //Show the dialog for adding withdraw amount for the current mane
            JDialog_AbstractEntry wd = new JDialog_Withdraw(this, accnr);
            wd.setBounds(430, 15, 275, 140);
            wd.show();

            // compute new amount
            Double deposit = Double.parseDouble(amountDeposit);
            String accNumber = (String) model.getValueAt(selection, 0);
            ff.addWithdrawalEntry(accNumber, deposit);

        }


    }

    void JButtonAddinterest_actionPerformed(java.awt.event.ActionEvent event) {
         ff.addInterstForAllAccounts();
    }

    void JButtonGenBill_actionPerformed(ActionEvent event) {
        int selection = JTable1.getSelectionModel().getMinSelectionIndex();
        if (selection >= 0) {
            IAccount acc = null;
            String accNumber = (String) model.getValueAt(selection, 0);
            acc = ff.getAccount(Integer.parseInt(accNumber));
            JDialogGenBill billFrm = new JDialogGenBill(acc);
            billFrm.setBounds(450, 20, 400, 350);
            billFrm.show();
        }
    }

    @Override
    //redraw the table
    public void update(Observable o, Object arg) {
        System.out.println("bank form is updated");
        IAccount acc = (IAccount) o;
        for (int i = 0; i < model.getRowCount(); i++) {
            if (model.getValueAt(i, 0).equals(String.valueOf(acc.getAccountNumber()))) {
                model.setValueAt(acc.getBalance().toString(), i, 5);
                break;
            }
        }
    }
}
